<template>
	<view class="page_reg">
		<form class="form-input" @submit="formSubmit" >
			<view>
				<input class="input-phone" name="phoneNumber" type="text" placeholder="手机号" v-model="PHONE"/>
				<input class="input-phone" name="invitephone" type="text" placeholder="推荐人手机号" v-model="invitePHONE"/>
			</view>
			
			<view>
				<button class="submit" type="primary" formType="submit"  >下一步</button>
			</view>
			
			
		</form>
		<text class="tip">我们将向您的手机发送验证短信</text>
	</view>
</template>

<script>
	//来自 graceUI 的表单验证， 使用说明见手册 http://grace.hcoder.net/doc/info/73-3.html
	var  graceChecker = require("../../common/graceChecker.js");
	export default {
		data() {
			return {
				number: '',
				PHONE: '',
				userId: '',
				invitePHONE:''
			};
		},
		onLoad(options){
			//#ifdef H5
			if(options.userId){
				console.log("该人员有邀请人///////邀请人id是",options.userId);
				this.userId = options.userId;
			}
			//#endif
		uni.startPullDownRefresh();
		},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		watch:{
			number(val) {
				this.PHONE = this.number
			},
			
		},
		
		methods: {
			
			//表单校验
			formSubmit(e) {
				// 将下列代码加入到对应的检查位置
				// 定义表单规则
				var rule = [
					{name:"phoneNumber", checkType : "phoneno",  errorMsg:"请输入正确的手机号"},
					// {name:"invitephone", checkType : "phoneno",  errorMsg:"请输入正确的邀请人手机号"},
				];
				//进行表单检查
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if(checkRes){
					console.log(">>>>>>>>>>",e.detail.value)
					this.PHONE = e.detail.value.phoneNumber
					if(e.detail.value.invitephone){
							//查询邀请人ID
							this.$http.post('/user/findUserid',{
								PHONE : e.detail.value.invitephone,
								SIGNATURE : uni.getStorageSync("SIGNATURE"),
							}).then((res1)=>{
								//判断返回内容
								if(res1.data.STATE === "0" && res1.statusCode === 200){
										//页面跳转     有邀请人的注册跳转
										 uni.navigateTo({
											 url : '/pages/reg-pwd/reg-pwd?PHONE='+ this.PHONE +"&userId="+ res1.data.OBJECT
										})
								}else{
									if(res1.data.MSG!=''){
										uni.showToast({
											title: res1.data.MSG,
											icon: "none",
										})
									}
								}
							})
							.catch((err)=>{
							    console.log('request fail', err);
							})
					}else{
						this.$http.post('/common/queryDefaultInvite',{
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
						}).then((res)=>{
							//判断返回内容
							if(res.data.STATE === "0" && res.statusCode === 200){
									//查询邀请人ID
									this.$http.post('/user/findUserid',{
										PHONE : res.data.OBJECT,
										SIGNATURE : uni.getStorageSync("SIGNATURE"),
									}).then((res1)=>{
										//判断返回内容
										if(res1.data.STATE === "0" && res1.statusCode === 200){
												//页面跳转     有邀请人的注册跳转
												 uni.navigateTo({
													 url : '/pages/reg-pwd/reg-pwd?PHONE='+ this.PHONE +"&userId="+ res1.data.OBJECT
												})
										}else{
											if(res1.data.MSG!=''){
												uni.showToast({
													title: res1.data.MSG,
													icon: "none",
												})
											}
										}
									})
									.catch((err)=>{
									    console.log('request fail', err);
									})
							}else{
								if(res.data.MSG!=''){
									uni.showToast({
										title: res.data.MSG,
										icon: "none",
									})
								}
							}
						})
						.catch((err)=>{
						    console.log('request fail', err);
						})
					}
				
					
					
					
					//#ifndef H5   
					//页面跳转   
					//  uni.navigateTo({
					// 	 url : '/pages/reg-pwd/reg-pwd?PHONE='+ this.PHONE
					// })
					//#endif
					//#ifdef H5
					// if(this.userId){
					
					// }else{
					// 	// 页面跳转   
					// 	 uni.navigateTo({
					// 		 url : '/pages/reg-pwd/reg-pwd?PHONE='+ this.PHONE
					// 	})
					// }
					//#endif
					
				}else{
					uni.showToast({ title: graceChecker.error, icon: "none" });
				}
			}
		},
	}
</script>

<style>
	page {
		height: auto;
		min-height: 100%;
		background-color: #f5f6f8;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>
<style lang="scss" scoped="">
	$form-border-color: rgba(214, 214, 214, 1);
	$text-color: #B6B6B6;
	
	.page_reg {
		width: 90%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.form-input{
		width: 90%;
		
	}
	.input-phone{
		margin-top: 40upx;
		width: 100%;
		border-bottom: 1px solid $form-border-color;
	}
	
	.submit {
		width: 100%;
		margin: 30px 0upx 100upx;
		color: white;
		background-color:#E02E24;
		-webkit-tap-highlight-color: rgba(252, 44, 93, 1.0);
		
		&:active {
			color: #B6B6B6;
			background-color: rgba(252, 44, 93, 0.8);
		}
	}
	
	.tip {
		font-size: 13px;
		color: $text-color;
	}
</style>
